﻿Imports BLL
Imports EntitiesLayer

'---------------------------------------> Clase creada por Diego Barillas <----------------------------------------

Public Class frmConsultarRoles

    Public numero As Integer 'Variable que almacena la fila seleccionada del dataGrid
    Public listaPermisos As List(Of Integer)


    Private Sub frmConsultarRoles_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        tblRoles.DataSource = objGestor.listarRoles()
        tblRoles.Columns(0).Visible = False
        tblRoles.Columns(3).Visible = False
        tblRoles.Columns(1).HeaderText = "Nombre"
        tblRoles.Columns(2).HeaderText = "Descripción"
    End Sub



    Private Sub btnRegistrarRol_Click(sender As Object, e As EventArgs) Handles btnRegistrarRol.Click
        FrmPrincipal.AddOwnedForm(frmTransparente)
        Dim frm As New frmRegistrarRol
        frm.Dock = DockStyle.Fill

        frmTransparente.AgregarContenido(frm)
        frmTransparente.Show()
    End Sub

    Private Sub tblRoles_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles tblRoles.CellClick
        numero = tblRoles.CurrentRow.Index 'se guarda en la variable la fila seleccionada del dataGrid
    End Sub

    Private Sub btnInfoRol_Click(sender As Object, e As EventArgs) Handles btnInfoRol.Click
        Me.Controls.Clear()
        Dim frm As New Control
        frm = New frmInformacionRoles
        frm.Dock = DockStyle.Fill
        Me.Controls.Add(frm)
        Dim rol As Rol = objGestor.buscarRol(tblRoles.Item(0, numero).Value)  'Carga el rol seleccionado en el data grid

        'recorre los textBox del user control informacion del rol y setea la información
        For Each x As frmInformacionRoles In Me.Controls.OfType(Of frmInformacionRoles)()
            For Each y As TextBox In x.Controls.OfType(Of TextBox)()
                If (y.Name = "txtNombre") Then
                    y.Text = rol.Nombre
                ElseIf (y.Name = "txtDescripcion") Then
                    y.Text = rol.Descripcion
                ElseIf (y.Name = "txtIdRol") Then
                    y.Text = rol.Id
                End If
            Next
            For Each y As RichTextBox In x.Controls.OfType(Of RichTextBox)()
                If (y.Name = "txtDescripcion") Then
                    y.Text = rol.Descripcion
                End If
            Next

            For Each panel As Panel In x.Controls.OfType(Of Panel)()

                If panel.Name = "panelCheckboxes" Then
                    For Each check As CheckBox In panel.Controls.OfType(Of CheckBox)()
                        listaPermisos = objGestor.listaPermisos(rol.Id)
                        For Each permiso As Integer In listaPermisos.OfType(Of Integer)()
                            If check.AccessibleName = permiso Then
                                check.Checked = True
                            End If
                        Next
                    Next

                End If


            Next


        Next
    End Sub


    Private Sub txtFiltrarTabla_TextChanged(sender As Object, e As EventArgs) Handles txtFiltrarTabla.TextChanged

        Dim texto As String = txtFiltrarTabla.Text 'guarda el valor escrito en el textBox
        Dim listaFiltrada As IEnumerable = objGestor.filtrarListaRoles(texto) 'llama el metodo filtrarLista del gestor que devuelve una lista de roles
        tblRoles.DataSource = listaFiltrada 'Carga la lista en el dataGrid

        'validación para que si la lista contiene un valor muestre el data grid con las propiedades
        If listaFiltrada IsNot Nothing Then

            tblRoles.Columns(0).Visible = False
            tblRoles.Columns(3).Visible = False
            tblRoles.Columns(1).HeaderText = "Nombre"
            tblRoles.Columns(2).HeaderText = "Descripción"
        Else
            tblRoles.DataSource = Nothing
        End If
    End Sub

End Class
